home *** CD-ROM | disk | FTP | other *** search
/ PC Format (PL) 2008 December / PC_Format_122008.iso / Multimedia / MediaPortal 0.2.3.0 / MediaPortal_0.2.3.0_Setup.exe / scripts / imdb / edb_co_il.csscript next >
Text File  |  2007-10-31  |  17KB  |  531 lines

  1. ∩╗┐//css_reference "core.dll";
  2. //css_reference "Databases.dll";
  3. //css_reference "utils.dll";
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using System.IO;
  13. using System.Net;
  14. using System.Collections;
  15. using System.Web;
  16. using System.Text.RegularExpressions;
  17. using MediaPortal.Util;
  18.  
  19. class Grabber : MediaPortal.Video.Database.IIMDBScriptGrabber
  20. {
  21.   public Grabber()
  22.     {
  23.     }
  24.   void MediaPortal.Video.Database.IIMDBScriptGrabber.FindFilm(string strSearch, int iLimit, ArrayList elements)
  25.   {
  26.     int iCount = 0;
  27.     string strTitle;
  28.     try
  29.     {
  30.       string absoluteUri;
  31.       string strURL = "http://www.edb.co.il/Find.php?q=" + strSearch;
  32.       string strBody = GetPage(strURL, "windows-1255", out absoluteUri);
  33.       
  34.       int iStartOfMovieList = strBody.IndexOf("╫¢╫ò╫¬╫¿╫Ö╫¥ ╫á╫ñ╫ò╫ª╫Ö╫¥");
  35.       
  36.         if (iStartOfMovieList < 0)
  37.         {
  38.           return;
  39.         }
  40.  
  41.       int iEndOfMovieList = strBody.IndexOf("</div><br />", iStartOfMovieList);
  42.  
  43.       if (iEndOfMovieList < 0)
  44.       {
  45.         iEndOfMovieList = strBody.Length;
  46.       }
  47.       
  48.       strBody = strBody.Substring(iStartOfMovieList, iEndOfMovieList - iStartOfMovieList);
  49.       
  50.       while ((true) && (iCount < iLimit))
  51.       {
  52.         ////<A HREF="/Title?0167261">Lord of the Rings: The Two Towers, The (2002)</A>
  53.         int iAHREF = strBody.IndexOf("<a href=");
  54.         if (iAHREF >= 0)
  55.         {
  56.           int iEndAHREF = strBody.IndexOf("</a>");
  57.           if (iEndAHREF >= 0)
  58.           {
  59.             iAHREF += "<a href=.".Length;
  60.             string strAHRef = strBody.Substring(iAHREF, iEndAHREF - iAHREF);
  61.             int iURL = strAHRef.IndexOf(">");
  62.             if (iURL > 0)
  63.             {
  64.               strTitle = "";
  65.               strURL = strAHRef.Substring(0, iURL);
  66.               if (strURL[strURL.Length - 1] == '\'')
  67.                 strURL = strURL.Substring(0, strURL.Length - 1);
  68.  
  69.               iURL+="<span style='direction: rtl;'>".Length + 1;
  70.               int iURLEnd = strAHRef.IndexOf("</span>", iURL);
  71.               if (iURLEnd > 0)
  72.               {
  73.                 strTitle = strAHRef.Substring(iURL, iURLEnd - iURL);
  74.                 iURLEnd = strAHRef.IndexOf("(", iURLEnd);
  75.                 if (iURLEnd > 0)
  76.                 {
  77.                     iURL = iURLEnd;
  78.                     iURLEnd = strAHRef.IndexOf(")", iURLEnd);
  79.                     if (iURLEnd > 0)
  80.                     {
  81.                         strTitle+= " " + strAHRef.Substring(iURL, iURLEnd - iURL + 1);
  82.                     }
  83.                 }
  84.               }
  85.               else
  86.                 strTitle = strAHRef.Substring(iURL);
  87.               
  88.               strURL = String.Format("http://www.edb.co.il{0}", strURL);
  89.               
  90.               HTMLUtil htmlUtil = new HTMLUtil();
  91.               htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  92.  
  93.               int endTagLength = "</a>".Length;
  94.               int posNextTag = strBody.IndexOf("<", iEndAHREF + endTagLength);
  95.               if (posNextTag > 0)
  96.               {
  97.                 string strSub = strBody.Substring(iEndAHREF + endTagLength, posNextTag - (iEndAHREF + endTagLength));
  98.                 strTitle += strSub;
  99.               }
  100.               
  101.               // to avoid including of   
  102.               if ((strTitle.IndexOf("\n") < 0) && (strTitle.IndexOf(" ") < 0))
  103.               {
  104.                 MediaPortal.Video.Database.IMDB.IMDBUrl url = new MediaPortal.Video.Database.IMDB.IMDBUrl(strURL, strTitle + " - ╫₧╫¬╫ò╫Ü edb.co.il", "edb_co_il");
  105.                 elements.Add(url);
  106.               }
  107.               iCount++;
  108.             }
  109.             if (iEndAHREF + 1 >= strBody.Length)
  110.               break;
  111.             iStartOfMovieList = iEndAHREF + 1;
  112.             strBody = strBody.Substring(iEndAHREF + 1);
  113.           }
  114.           else
  115.           {
  116.             break;
  117.           }
  118.         }
  119.         else
  120.         {
  121.           break;
  122.         }
  123.       }
  124.     }
  125.     catch (Exception ex)
  126.     {
  127.       MediaPortal.GUI.Library.Log.Error("exception for edb lookup of {0} err:{1} stack:{2}",strSearch, ex.Message, ex.StackTrace);
  128.     }
  129.   }
  130.  
  131.   bool MediaPortal.Video.Database.IIMDBScriptGrabber.GetDetails(MediaPortal.Video.Database.IMDB.IMDBUrl url, ref MediaPortal.Video.Database.IMDBMovie movieDetails)
  132.   {
  133.     try
  134.     {
  135.  
  136.       int iStart = 0;
  137.       int iEnd = 0;
  138.       movieDetails.Reset();
  139.       // add databaseinfo
  140.       // may add an another grabber  
  141.       movieDetails.Database = "edb_co_il";
  142.  
  143.       string strAbsURL;
  144.       string strBody = GetPage(url.URL, "windows-1255", out strAbsURL);
  145.       if (strBody == null || strBody.Length == 0)
  146.         return false;
  147.  
  148.       // IMDBNumber
  149.       int iPos = strAbsURL.IndexOf("/titles/");
  150.       if (iPos > 0)
  151.       {
  152.         iPos += "/titles/".Length;
  153.         movieDetails.IMDBNumber = strAbsURL.Substring(iPos);
  154.         int pos = movieDetails.IMDBNumber.IndexOf("/");
  155.         if (pos > 0)
  156.           movieDetails.IMDBNumber = movieDetails.IMDBNumber.Substring(0, pos);
  157.       }
  158.  
  159.       // Title
  160.       url.Title = url.Title.Trim();
  161.       iEnd = url.Title.IndexOf("(");
  162.       if (iEnd >= 0)
  163.       {
  164.           int yearEnd = url.Title.IndexOf(")");
  165.           if (yearEnd >= 0)
  166.           {
  167.               try
  168.               {
  169.                   movieDetails.Year = int.Parse(url.Title.Substring(iEnd+1, yearEnd - iEnd - 1));
  170.               }
  171.               catch (Exception)
  172.               {
  173.               }
  174.           }
  175.         movieDetails.Title = url.Title.Substring(0, iEnd);
  176.       }
  177.       else
  178.         movieDetails.Title = url.Title;
  179.  
  180.       string RegTitleBlock = "<p class='ph1_title'.*>(.*)</p>";
  181.       Match titleBlock = Regex.Match(strBody, RegTitleBlock);
  182.       if (titleBlock.Success)
  183.       {
  184.         // I used the MPARating string as a subtitle (because it appears in the mediaportal below the title)
  185.           movieDetails.MPARating = movieDetails.Title.Trim();
  186.           movieDetails.PlotOutline = movieDetails.Title.Trim();
  187.           
  188.           movieDetails.Title = titleBlock.Groups[1].Value;
  189.       }
  190.       else
  191.       {
  192.           movieDetails.Title = movieDetails.Title.Trim();
  193.           movieDetails.PlotOutline = movieDetails.Title.Trim();
  194.       }
  195.       
  196.       movieDetails.TagLine = "";
  197.       
  198.       // Plot
  199.       String PLOT_START="<div class='fieldtitle'>╫ó╫£╫Ö╫£╫ö</div>";
  200.       String PLOT_END="</div>";
  201.       int plotPos = strBody.IndexOf(PLOT_START);
  202.       if (plotPos >= 0)
  203.       {
  204.           plotPos+=PLOT_START.Length;
  205.           int plotEndPos = strBody.IndexOf(PLOT_END, plotPos);
  206.           if (plotEndPos >= 0)
  207.           {
  208.               movieDetails.Plot = strBody.Substring(plotPos, plotEndPos - plotPos).Trim().Replace("<div>","").Replace("<br />","\n").Replace("\\'","\"");
  209.           }
  210.       }
  211.       
  212.       // Votes
  213.       String VOTE_REG = "alt='(\\d*) ╫Æ╫ò╫£╫⌐╫Ö╫¥ ╫ô╫Ö╫¿╫Æ╫ò: (\\d\\.?\\d*) ╫¢╫ò╫¢╫æ╫Ö╫¥' />";
  214.       Match voteBlock = Regex.Match(strBody, VOTE_REG);
  215.       if (voteBlock.Success)
  216.       {
  217.         movieDetails.Votes = voteBlock.Groups[1].Value;
  218.         try
  219.         {
  220.             movieDetails.Rating = float.Parse(voteBlock.Groups[2].Value);
  221.         }
  222.         catch (Exception ex2)
  223.         {
  224.             MediaPortal.GUI.Library.Log.Error("exception for edb lookup of {0} err:{1} stack:{2}", url.URL, ex2.Message, ex2.StackTrace);
  225.         }    
  226.       }
  227.       
  228.       // Runtime
  229.       String RUNTIME_REG= "<p><span class='fieldname'>╫û╫₧╫ƒ ╫¿╫Ö╫ª╫ö:</span> (\\d*) ╫ô╫º╫ò╫¬.</p>";
  230.       Match runtimeBlock = Regex.Match(strBody, RUNTIME_REG);
  231.       if (runtimeBlock.Success)
  232.       {
  233.         try
  234.         {
  235.             movieDetails.RunTime = int.Parse(runtimeBlock.Groups[1].Value);
  236.         }
  237.         catch (Exception ex3)
  238.         {
  239.             MediaPortal.GUI.Library.Log.Error("exception for edb lookup of {0} err:{1} stack:{2}", url.URL, ex3.Message, ex3.StackTrace);
  240.         }    
  241.       }
  242.       
  243.       // Genres
  244.       String GENRE_START="<p><span class='fieldname'>╫û'╫É╫á╫¿:</span>";
  245.       String GENRE_INSTANCE_START="<a href='/movies/browse.php?genre=";
  246.       String GENRE_INSTANCE_END="'>";
  247.       String GENRE_END="</p>";
  248.       int genrePos = strBody.IndexOf(GENRE_START);
  249.       if (genrePos >= 0)
  250.       {
  251.           genrePos+=GENRE_START.Length;
  252.           int genreEndPos = strBody.IndexOf(GENRE_END, genrePos);
  253.           if (genreEndPos >= 0)
  254.           {
  255.               String genreString = strBody.Substring(genrePos, genreEndPos - genrePos + 1);
  256.               
  257.               genrePos =  genreString.IndexOf(GENRE_INSTANCE_START);
  258.               movieDetails.Genre = "";
  259.               while (genrePos >= 0)
  260.               {
  261.                   genrePos+=GENRE_INSTANCE_START.Length;
  262.                   genreEndPos = genreString.IndexOf(GENRE_INSTANCE_END, genrePos);
  263.                   if (genreEndPos >= 0)
  264.                   {
  265.                       String genre = genreString.Substring(genrePos, genreEndPos - genrePos);
  266.                       movieDetails.Genre+=genre;
  267.                   }
  268.                   genrePos = genreString.IndexOf(GENRE_INSTANCE_START, genrePos);
  269.                   if (genrePos >= 0) 
  270.                   {
  271.                       movieDetails.Genre+=" / ";
  272.                   }
  273.             }
  274.           }         
  275.       }
  276.       
  277.       // Actors
  278.       String ACTOR_START="<div class='fieldtitle'>╫ª╫ò╫ò╫¬ ╫⌐╫ù╫º╫á╫Ö╫¥</div>";
  279.       String ACTOR_INSTANCE_START="<a href='/people/";
  280.       String ACTOR_INSTANCE_START2="/'>";
  281.       String ACTOR_INSTANCE_END="</a>";
  282.       String AS_START="....";
  283.       String AS_END="</td>";
  284.       String ACTOR_END="</table>";
  285.       int actorPos = strBody.IndexOf(ACTOR_START);
  286.       if (actorPos >= 0)
  287.       {
  288.           actorPos+=ACTOR_START.Length;
  289.           int actorEndPos = strBody.IndexOf(ACTOR_END, actorPos);
  290.           if (actorEndPos >= 0)
  291.           {
  292.               String actorString = strBody.Substring(actorPos, actorEndPos - actorPos + 1);
  293.               actorPos =  actorString.IndexOf(ACTOR_INSTANCE_START);
  294.               movieDetails.Cast = "";
  295.               while (actorPos >= 0)
  296.               {
  297.                   actorPos+=ACTOR_INSTANCE_START.Length;
  298.                   actorPos =  actorString.IndexOf(ACTOR_INSTANCE_START2, actorPos);
  299.                   if (actorPos >= 0)
  300.                   {
  301.                       actorPos+=ACTOR_INSTANCE_START2.Length;
  302.                       actorEndPos = actorString.IndexOf(ACTOR_INSTANCE_END, actorPos);
  303.                       if (actorEndPos >= 0)
  304.                       {
  305.                           String actor = actorString.Substring(actorPos, actorEndPos - actorPos);
  306.                           movieDetails.Cast+=actor;
  307.                           int asPos = actorString.IndexOf(AS_START, actorPos);
  308.                           if (asPos >= 0)
  309.                           {
  310.                               asPos+=AS_START.Length;
  311.                               int asEndPos = actorString.IndexOf(AS_END, asPos);
  312.                               if (asEndPos >= 0)
  313.                               {
  314.                                   String asStr = actorString.Substring(asPos, asEndPos - asPos).Trim();
  315.                                   movieDetails.Cast+=" as "+asStr;
  316.                               }
  317.                           }
  318.                       }
  319.                       actorPos = actorString.IndexOf(ACTOR_INSTANCE_START, actorPos);
  320.                       if (actorPos >= 0) 
  321.                       {
  322.                           movieDetails.Cast+="\n";
  323.                       }
  324.                   }
  325.             }
  326.           }    
  327.       }
  328.           
  329.       // Director
  330.       String DIRECTOR_START="<div class='columnfields_right'>╫æ╫Ö╫₧╫ò╫Ö</div>";
  331.       String DIRECTOR_END="</div>";
  332.       
  333.       int directorPos = strBody.IndexOf(DIRECTOR_START);
  334.       if (directorPos >= 0)
  335.       {
  336.           directorPos+=DIRECTOR_START.Length;
  337.           int directorEndPos = strBody.IndexOf(DIRECTOR_END, directorPos);
  338.           if (directorEndPos >= 0)
  339.           {
  340.               String directorLine = strBody.Substring(directorPos, directorEndPos - directorPos);
  341.               
  342.               String DIRECTOR_REG= "<a href=\"/people/pp\\d*/\">(.*)</a>";
  343.               Match directorBlock = Regex.Match(directorLine, DIRECTOR_REG);
  344.               movieDetails.Director = "";
  345.               while (directorBlock.Success)
  346.               {
  347.                 movieDetails.Director+= directorBlock.Groups[1].Value;
  348.                 directorBlock = directorBlock.NextMatch();
  349.                 if (directorBlock.Success)
  350.                 {
  351.                     movieDetails.Director+=", ";
  352.                 }
  353.               }
  354.           }
  355.       }
  356.       
  357.       // WritingCredits
  358.       String WRITER_START="<div class='columnfields_right'>╫¬╫í╫¿╫Ö╫ÿ</div>";
  359.       String WRITER_END="</div>";
  360.       
  361.       int writerPos = strBody.IndexOf(WRITER_START);
  362.       if (writerPos >= 0)
  363.       {
  364.           writerPos+=WRITER_START.Length;
  365.           int writerEndPos = strBody.IndexOf(WRITER_END, writerPos);
  366.           if (writerEndPos >= 0)
  367.           {
  368.               String writerLine = strBody.Substring(writerPos, writerEndPos - writerPos);
  369.              
  370.               String WRITER_REG= "<a href=\"/people/pp\\d*/\">(.*)</a>";
  371.               Match writerBlock = Regex.Match(writerLine, WRITER_REG);
  372.               movieDetails.WritingCredits = "";
  373.               while (writerBlock.Success)
  374.               {
  375.                 movieDetails.WritingCredits+= writerBlock.Groups[1].Value;
  376.                 writerBlock = writerBlock.NextMatch();
  377.                 if (writerBlock.Success)
  378.                 {
  379.                     movieDetails.WritingCredits+=", ";
  380.                 }
  381.               }
  382.           }
  383.       }
  384.       
  385.       return true;
  386.     }
  387.     catch (Exception ex)
  388.     {
  389.       MediaPortal.GUI.Library.Log.Error("exception for edb lookup of {0} err:{1} stack:{2}", url.URL, ex.Message, ex.StackTrace);
  390.     }
  391.     return false;
  392.   }
  393.  
  394.   string MediaPortal.Video.Database.IIMDBScriptGrabber.GetName()
  395.   {
  396.     return "EDB";
  397.   }
  398.  
  399.   string MediaPortal.Video.Database.IIMDBScriptGrabber.GetLanguage()
  400.   {
  401.     return "Hebrew";
  402.   }
  403.  
  404.   private string GetPage(string strURL, string strEncode, out string absoluteUri)
  405.   {
  406.     string strBody = "";
  407.     absoluteUri = String.Empty;
  408.     Stream ReceiveStream = null;
  409.     StreamReader sr = null;
  410.     WebResponse result = null;
  411.     try
  412.     {
  413.       // Make the Webrequest
  414.       //Log.Info("EDB: get page:{0}", strURL);
  415.       WebRequest req = WebRequest.Create(strURL);
  416.  
  417.       result = req.GetResponse();
  418.       ReceiveStream = result.GetResponseStream();
  419.  
  420.       // Encoding: depends on selected page
  421.       Encoding encode = System.Text.Encoding.GetEncoding(strEncode);
  422.       sr = new StreamReader(ReceiveStream, encode);
  423.       strBody = sr.ReadToEnd();
  424.  
  425.       absoluteUri = result.ResponseUri.AbsoluteUri;
  426.     }
  427.     catch (Exception)
  428.     {
  429.       //Log.Error("Error retreiving WebPage: {0} Encoding:{1} err:{2} stack:{3}", strURL, strEncode, ex.Message, ex.StackTrace);
  430.     }
  431.     finally
  432.     {
  433.       if (sr != null)
  434.       {
  435.         try
  436.         {
  437.           sr.Close();
  438.         }
  439.         catch (Exception)
  440.         {
  441.         }
  442.       }
  443.       if (ReceiveStream != null)
  444.       {
  445.         try
  446.         {
  447.           ReceiveStream.Close();
  448.         }
  449.         catch (Exception)
  450.         {
  451.         }
  452.       }
  453.       if (result != null)
  454.       {
  455.         try
  456.         {
  457.           result.Close();
  458.         }
  459.         catch (Exception)
  460.         {
  461.         }
  462.       }
  463.     }
  464.     return strBody;
  465.   } // END GetPage()
  466.   string ParseAHREFIMDB(string strBody, int iahref, string strURL)
  467.   {
  468.     int iStart = strBody.IndexOf("<a href=\"", iahref);
  469.     if (iStart < 0)
  470.       iStart = strBody.IndexOf("<A HREF=\"", iahref);
  471.     if (iStart < 0)
  472.       return "";
  473.  
  474.     int iEnd = strBody.IndexOf("</a>", iStart);
  475.     if (iEnd < 0)
  476.       iEnd = strBody.IndexOf("</A>", iStart);
  477.     if (iEnd < 0)
  478.       return "";
  479.  
  480.     iStart += "<a href=\"".Length;
  481.     int iSep = strBody.IndexOf(">", iStart);
  482.     string strurl = strBody.Substring(iStart, (iSep - iStart) - 1);
  483.     iSep++;
  484.     string strTitle = strBody.Substring(iSep, iEnd - iSep);
  485.     strTitle = MediaPortal.Util.Utils.stripHTMLtags(strTitle);
  486.     HTMLUtil htmlUtil = new HTMLUtil();
  487.     htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  488.     strTitle = strTitle.Trim();
  489.     return strTitle.Trim();
  490.  
  491.   }
  492.   string ParseGenresIMDB(string strBody, int iGenre, string url)
  493.   {
  494.     string strTmp;
  495.     string strTitle = "";
  496.     string strHRef = strBody.Substring(iGenre);
  497.     int iSlash = strHRef.IndexOf(" / ");
  498.     int iEnd = 0;
  499.     int iStart = 0;
  500.     if (iSlash >= 0)
  501.     {
  502.       int iRealEnd = strHRef.IndexOf(">more<");
  503.       if (iRealEnd < 0)
  504.         iRealEnd = strHRef.IndexOf("</div>");
  505.       while (iSlash < iRealEnd)
  506.       {
  507.         iStart = iEnd + 2;
  508.         iEnd = iSlash;
  509.         int iLen = iEnd - iStart;
  510.         if (iLen < 0)
  511.           break;
  512.         strTmp = strHRef.Substring(iStart, iLen);
  513.         strTitle = strTitle + ParseAHREFIMDB(strTmp, 0, "") + " / ";
  514.  
  515.         iSlash = strHRef.IndexOf(" / ", iEnd + 2);
  516.         if (iSlash < 0)
  517.           iSlash = iRealEnd;
  518.       }
  519.     }
  520.     // last genre
  521.     iEnd += 2;
  522.     strTmp = strHRef.Substring(iEnd);
  523.     strTitle = strTitle + ParseAHREFIMDB(strTmp, 0, "");
  524.     HTMLUtil htmlUtil = new HTMLUtil();
  525.     htmlUtil.ConvertHTMLToAnsi(strTitle, out strTitle);
  526.  
  527.     return strTitle;
  528.   }
  529.  
  530. }
  531.